home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gpmisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  2.6 KB  |  96 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gpmisc.c,v 1.7.2.1 2000/11/09 21:18:00 rayjj Exp $ */
  20. /* Miscellaneous support for platform facilities */
  21.  
  22. #include "close_.h"
  23. #include "fcntl_.h"
  24. #include "stdio_.h"
  25. #include "stat_.h"
  26. #include "gp.h"
  27. #include "gpgetenv.h"
  28. #include "gpmisc.h"
  29.  
  30. /*
  31.  * Get the name of the directory for temporary files, if any.  Currently
  32.  * this checks the TMPDIR and TEMP environment variables, in that order.
  33.  * The return value and the setting of *ptr and *plen are as for gp_getenv.
  34.  */
  35. int
  36. gp_gettmpdir(char *ptr, int *plen)
  37. {
  38.     int max_len = *plen;
  39.     int code = gp_getenv("TMPDIR", ptr, plen);
  40.  
  41.     if (code != 1)
  42.     return code;
  43.     *plen = max_len;
  44.     return gp_getenv("TEMP", ptr, plen);
  45. }
  46.  
  47. /*
  48.  * Open a temporary file, using O_EXCL and S_I*USR to prevent race
  49.  * conditions and symlink attacks.
  50.  */
  51. FILE *
  52. gp_fopentemp(const char *fname, const char *mode)
  53. {
  54.     int flags = O_EXCL;
  55.     /* Scan the mode to construct the flags. */
  56.     const char *p = mode;
  57.     int fildes;
  58.     FILE *file;
  59.  
  60.     while (*p)
  61.     switch (*p++) {
  62.     case 'a':
  63.         flags |= O_CREAT | O_APPEND;
  64.         break;
  65.     case 'r':
  66.         flags |= O_RDONLY;
  67.         break;
  68.     case 'w':
  69.         flags |= O_CREAT | O_WRONLY | O_TRUNC;
  70.         break;
  71. #ifdef O_BINARY
  72.         /* Watcom C insists on this non-ANSI flag being set. */
  73.     case 'b':
  74.         flags |= O_BINARY;
  75.         break;
  76. #endif
  77.     case '+':
  78.         flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  79.         break;
  80.     default:        /* e.g., 'b' */
  81.         break;
  82.     }
  83.     fildes = open(fname, flags, S_IRUSR | S_IWUSR);
  84.     if (fildes < 0)
  85.     return 0;
  86.     /*
  87.      * The DEC VMS C compiler incorrectly defines the second argument of
  88.      * fdopen as (char *), rather than following the POSIX.1 standard,
  89.      * which defines it as (const char *).  Patch this here.
  90.      */
  91.     file = fdopen(fildes, (char *)mode); /* still really const */
  92.     if (file == 0)
  93.     close(fildes);
  94.     return file;
  95. }
  96.